using System;
using System.IO;
using System.Reflection;
using System.Text.RegularExpressions;

using ObjectRelationalMapping.Model;

namespace ObjectRelationalMapping.Helper
{
    internal static partial class Common
    {
        #region Methods

        internal static string RemovePrefix ( string originalValue )
        {
            string result = originalValue;

            foreach ( Mapping excludedPrefix in ExcludedPrefixCollection )
            {
                if ( originalValue.Contains ( excludedPrefix.Name ) )
                {
                    result = 
                        originalValue.Replace ( 
                            excludedPrefix.Name, 
                            string.Empty );
                    break;
                }
            }

            return result;
        }

        internal static void SetExcludedPrefixCollection ( string excludedPrefixes )
        {
            if ( excludedPrefixes != string.Empty )
            {
                foreach ( string excludedPrefix in excludedPrefixes.Split ( ',' ) )
                {
                    ExcludedPrefixCollection.Add ( new Mapping ( excludedPrefix ) );
                }
            }
        }

        internal static void SetExcludedTableCollection ( string excludedTables )
        {
            if ( excludedTables != string.Empty )
            {
                foreach ( string excludedTable in excludedTables.Split ( ',' ) )
                {
                    ExcludedTableCollection.Add ( new Mapping ( excludedTable ) );
                }
            }
        }

        #endregion

        #region Properties

        private static string GetManifestResourceContents (
            string manifestResource )
        {
            Assembly currentAssembly = Assembly.GetExecutingAssembly ();
            StreamReader resourceStreamReader =
                new StreamReader (
                    currentAssembly.GetManifestResourceStream (
                        manifestResource ) );
            return resourceStreamReader.ReadToEnd ();
        }

        internal static string ApplicationNamespace
        {
            get { return applicationNamespace; }
            set { applicationNamespace = value; }
        }

        internal static string DatabaseConnectionString
        {
            get { return databaseConnectionString; }
            set { databaseConnectionString = value; }
        }

        internal static MappingCollection ExcludedPrefixCollection
        {
            get
            {
                if ( execludedPrefixCollection == null )
                {
                    execludedPrefixCollection = new MappingCollection ();
                }

                return execludedPrefixCollection;
            }
            set { execludedPrefixCollection = value; }
        }

        internal static MappingCollection ExcludedTableCollection
        {
            get
            {
                if ( execludedTableCollection == null )
                {
                    execludedTableCollection = new MappingCollection ();
                }

                return execludedTableCollection;
            }
            set { execludedTableCollection = value; }
        }

        internal static EntityCollection EntityCollection
        {
            get
            {
                if ( entityCollection == null )
                {
                    entityCollection = new EntityCollection ();
                }

                return entityCollection;
            }
            set { entityCollection = value; }
        }

        internal static bool ExecuteGeneratedSql
        {
            get { return executeGeneratedSql; }
            set { executeGeneratedSql = value; }
        }

        internal static string ProjectDirectory
        {
            get { return projectDirectory; }
            set { projectDirectory = value; }
        }

        internal static string ProjectDirectoryForClassObjects
        {
            get { return ProjectDirectory + @"Models\Generated\"; }
        }

        internal static string ProjectDirectoryForDatabaseObjects
        {
            get { return ProjectDirectoryForClassObjects + @"Sql\"; }
        }

        internal static string ProjectDirectoryForHelperObjects
        {
            get { return ProjectDirectoryForClassObjects + @"Helper\"; }
        }

        internal static string QueryColumnsCommand
        {
            get
            {
                if ( queryColumnsCommand == null
                    || queryColumnsCommand == string.Empty )
                {
                    queryColumnsCommand =
                        GetManifestResourceContents (
                            Constants.QueryColumnsTemplate );
                }

                return queryColumnsCommand;
            }
        }

        internal static string QueryPrimaryKeysCommand
        {
            get
            {
                if ( queryPrimaryKeysCommand == null
                    || queryPrimaryKeysCommand == string.Empty )
                {
                    queryPrimaryKeysCommand =
                        GetManifestResourceContents (
                            Constants.QueryPrimaryKeysTemplate );
                }

                return queryPrimaryKeysCommand;
            }
        }

        internal static string QueryTablesCommand
        {
            get
            {
                if ( queryTablesCommand == null
                    || queryTablesCommand == string.Empty )
                {
                    queryTablesCommand =
                        GetManifestResourceContents (
                            Constants.QueryTablesTemplate );
                }

                return queryTablesCommand;
            }
        }

        internal static string TemplateForClassObjects
        {
            get
            {
                if ( templateForClassObjects == null
                    || templateForClassObjects == string.Empty )
                {
                    templateForClassObjects =
                        GetManifestResourceContents (
                            Constants.ObjectClassTemplate );
                }

                return templateForClassObjects;
            }
        }

        internal static string TemplateForClassProperties
        {
            get
            {
                if ( templateForClassProperties == null
                    || templateForClassProperties == string.Empty )
                {
                    templateForClassProperties =
                        GetManifestResourceContents (
                            Constants.ObjectPropertyTemplate );
                }

                return templateForClassProperties;
            }
        }

        internal static string TemplateForDatabaseDelete
        {
            get
            {
                if ( templateForDatabaseDelete == null
                    || templateForDatabaseDelete == string.Empty )
                {
                    templateForDatabaseDelete =
                        GetManifestResourceContents (
                            Constants.DatabaseDeleteTemplate );
                }

                return templateForDatabaseDelete;
            }
        }

        internal static string TemplateForDatabaseInsert
        {
            get
            {
                if ( templateForDatabaseInsert == null
                    || templateForDatabaseInsert == string.Empty )
                {
                    templateForDatabaseInsert =
                        GetManifestResourceContents (
                            Constants.DatabaseInsertTemplate );
                }

                return templateForDatabaseInsert;
            }
        }

        internal static string TemplateForDatabasePaging
        {
            get
            {
                if ( templateForDatabasePaging == null
                    || templateForDatabasePaging == string.Empty )
                {
                    templateForDatabasePaging =
                        GetManifestResourceContents (
                            Constants.DatabasePagingTemplate );
                }

                return templateForDatabasePaging;
            }
        }

        internal static string TemplateForDatabaseSelect
        {
            get
            {
                if ( templateForDatabaseSelect == null
                    || templateForDatabaseSelect == string.Empty )
                {
                    templateForDatabaseSelect =
                        GetManifestResourceContents (
                            Constants.DatabaseSelectTemplate );
                }

                return templateForDatabaseSelect;
            }
        }

        internal static string TemplateForDatabaseUpdate
        {
            get
            {
                if ( templateForDatabaseUpdate == null
                    || templateForDatabaseUpdate == string.Empty )
                {
                    templateForDatabaseUpdate =
                        GetManifestResourceContents (
                            Constants.DatabaseUpdateTemplate );
                }

                return templateForDatabaseUpdate;
            }
        }

        internal static string TemplateForEntityClassObject
        {
            get
            {
                if ( templateForEntityClassObject == null
                    || templateForEntityClassObject == string.Empty )
                {
                    templateForEntityClassObject =
                        GetManifestResourceContents (
                            Constants.ObjectEntityTemplate );
                }

                return templateForEntityClassObject;
            }
        }

        internal static string TemplateForSqlHelperClassObject
        {
            get
            {
                if ( templateForSqlHelperClassObject == null
                    || templateForSqlHelperClassObject == string.Empty )
                {
                    templateForSqlHelperClassObject =
                        GetManifestResourceContents (
                            Constants.ObjectSqlHelperTemplate );
                }

                return templateForSqlHelperClassObject;
            }
        }

        private static string applicationNamespace;
        private static string databaseConnectionString;
        private static MappingCollection execludedPrefixCollection;
        private static MappingCollection execludedTableCollection;
        private static EntityCollection entityCollection;
        private static bool executeGeneratedSql;
        private static string projectDirectory;
        private static string queryColumnsCommand;
        private static string queryPrimaryKeysCommand;
        private static string queryTablesCommand;
        private static string templateForClassObjects;
        private static string templateForClassProperties;
        private static string templateForDatabaseDelete;
        private static string templateForDatabaseInsert;
        private static string templateForDatabasePaging;
        private static string templateForDatabaseSelect;
        private static string templateForDatabaseUpdate;
        private static string templateForEntityClassObject;
        private static string templateForSqlHelperClassObject;

        #endregion
    }
}